﻿Option Explicit On
Option Strict Off

Partial Class private_secure_members_manage_MyCampaignFilter
    Inherits System.Web.UI.Page

    Dim u As MembershipUser
    Dim personId As Int32
    Dim delid As Integer
    Dim entityid As Integer

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            grdCampaignFilterItem.Visible = False
        End If

        u = Membership.GetUser(HttpContext.Current.User.Identity.Name)
        Dim getpersonid As New GetPersonIdFromUserId()

        Try

            personId = getpersonid.GetPersonId(CType(u.ProviderUserKey, Guid))
            Dim entityidTA As New LogginsLogDSTableAdapters.usp_GetEntityListFull_ByPersonIdTableAdapter()
            Dim entityidDT As LogginsLogDS.usp_GetEntityListFull_ByPersonIdDataTable
            entityidDT = entityidTA.GetData(personId)

            Me.grdCampaignFilter.DataBind()

            'For Each entityidRow As LogginsLogDS.usp_GetEntityListFull_ByPersonIdRow In entityidDT
            '    index = entityidRow.ID
            'Next
            'Dim dateenteredtextbox As TextBox = CType(Me.FormView1.FindControl("DateCreatedTextBox"), TextBox)
            'dateenteredtextbox.Text = CStr(DateTime.Now)

        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Protected Sub SqlEntity_ds_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlEntity_ds.Selecting
        e.Command.Parameters("@PersonId").Value = CType(personId, Integer)
    End Sub


    Protected Sub btnAddCampaignFilter_Click(sender As Object, e As System.EventArgs) Handles btnAddCampaignFilter.Click
        'Nenad: Every Filter should have the default filter named 'No Filter' 
        'Nenad: SystemFilter.Id = 0
        'First Off call the stored procedure named dbo.Usp_CampaignFilterInsert 
        'EXEC [dbo].[usp_CampaignFilterInsert] @EntityId , @Name, @Note, @Active
        'Dim campaignfilterTA As New LogginsLogDSTableAdapters.usp_CampaignFilterTableAdapter()
        'campaignfilterTA.Insert(ddlEntity.SelectedValue, "No Filter", "", True)
        'grdCampaignFilter.DataBind()

        InsertPopupExtender.Show()
    End Sub


    'Protected Sub dvCampaignFilter_ItemDeleting(sender As Object, e As System.Web.UI.WebControls.DetailsViewDeleteEventArgs) Handles dvCampaignFilter.ItemDeleting
    '    Dim EntityId As Integer = CInt(Me.ddlEntity.SelectedValue)
    '    e.Values("EntityId") = CInt(EntityId)
    'End Sub

    'Protected Sub dvCampaignFilter_ItemInserting(sender As Object, e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles dvCampaignFilter.ItemInserting
    '    Dim EntityId As Integer = CInt(Me.ddlEntity.SelectedValue)
    '    e.Values("EntityId") = CInt(entityid)
    '    e.Values("Active") = 1
    'End Sub

    'Protected Sub dvCampaignFilter_ItemUpdating(sender As Object, e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles dvCampaignFilter.ItemUpdating
    '    Dim EntityId As Integer = CInt(Me.ddlEntity.SelectedValue)
    '    e.NewValues("Active") = 1
    'End Sub

    Protected Sub ddlEntity_DataBound(sender As Object, e As System.EventArgs) Handles ddlEntity.DataBound

    End Sub

    Protected Sub ddlEntity_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlEntity.SelectedIndexChanged
        If Not ddlEntity.SelectedValue.ToString() = "0" Then
            Dim campaignfilterTA As New LogginsLogDSTableAdapters.usp_CampaignFilterTableAdapter()
            Dim campaignfilterDT As LogginsLogDS.usp_CampaignFilterDataTable
            Dim campaignfilteraDT As LogginsLogDS.usp_CampaignFilterDataTable
            campaignfilterDT = campaignfilterTA.GetData(ddlEntity.SelectedValue)
            campaignfilteraDT = campaignfilterTA.GetData(Nothing)

            Dim cquery = From q In campaignfilteraDT.AsEnumerable() Where q.Name = "No Filter" AndAlso
                     q.EntityId = ddlEntity.SelectedValue Select q
            If cquery.Count = 0 Then
                campaignfilterTA.Insert(ddlEntity.SelectedValue, "No Filter", "", True)
            End If
            btnAddCampaignFilter.Visible = True

            grdCampaignFilter.DataBind()
        Else
            btnAddCampaignFilter.Visible = False
            divSelectedFilterText.Visible = False
            UpdatePanel4.Update()
        End If
    End Sub

    Protected Sub btnInsertCampainFilter_Click(sender As Object, e As System.EventArgs) Handles btnInsertCampainFilter.Click
        If Not ddlEntity.SelectedValue.ToString() = "0" Then
            If Not txtInsertName.Text = String.Empty Then
                Dim campaignfilterTA As New LogginsLogDSTableAdapters.usp_CampaignFilterTableAdapter()
                campaignfilterTA.Insert(ddlEntity.SelectedValue, txtInsertName.Text, txtInsertNote.Text, True)
                lblNameError.Text = ""
                txtInsertName.Text = ""
                txtInsertNote.Text = ""
                InsertPopupExtender.Hide()
                grdCampaignFilter.DataBind()
                UpdatePanel2.Update()
            Else
                lblNameError.Text = "Name is a required field."
            End If
        End If
    End Sub

    Protected Sub grdCampaignFilter_DataBound(sender As Object, e As System.EventArgs) Handles grdCampaignFilter.DataBound
        'UpdatePanel3.Update()
        If Not grdCampaignFilter.SelectedValue Is Nothing Then
            grdCampaignFilterItem.Visible = True
            UpdatePanel3.Update()
            divSelectedFilterText.Visible = True
            UpdatePanel4.Update()

        Else
            grdCampaignFilterItem.Visible = False
            UpdatePanel3.Update()
            divSelectedFilterText.Visible = False
            UpdatePanel4.Update()

        End If

    End Sub

    Protected Sub grdCampaignFilterItem_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdCampaignFilterItem.RowCommand

        If Not grdCampaignFilter.SelectedValue Is Nothing Then
            If e.CommandName.Equals("EmptyInsert") Then
                Dim newSeqNo As TextBox = TryCast(grdCampaignFilterItem.Controls(0).Controls(0).FindControl("txtNewInsertSeqNo"), TextBox)
                Dim newSystemFilter As DropDownList = TryCast(grdCampaignFilterItem.Controls(0).Controls(0).FindControl("ddlInsertNewSystemFilter"), DropDownList)
                Dim newAndOr As TextBox = TryCast(grdCampaignFilterItem.Controls(0).Controls(0).FindControl("txtInsertNewAndOr"), TextBox)
                Try
                    If Not newSeqNo.Text = "" Then
                        If Not newAndOr.Text = "" Then
                            Dim campaignfilteritemTA As New LogginsLogDSTableAdapters.usp_CampaignFilterItemTableAdapter()
                            campaignfilteritemTA.Insert(grdCampaignFilter.SelectedValue, newSeqNo.Text, newSystemFilter.SelectedValue, newAndOr.Text, True)
                            ''Here
                            Dim seqnofixTA As New LogginsLogDSTableAdapters.SPTableAdapter()
                            seqnofixTA.usp_CampaignFilterItem_FixSeqNo(grdCampaignFilter.SelectedValue)
                            grdCampaignFilterItem.DataBind()
                        Else
                            lblMessage.Text = "Please enter AndOr value"
                            InsertPopupExtender0.Show()
                            lblAction0.Text = "Please enter AndOr value"
                        End If


                    Else
                        lblMessage.Text = "Please enter sequence number"
                        InsertPopupExtender0.Show()
                        lblAction0.Text = "Please enter sequence number"

                    End If

                    UpdatePanel3.Update()
                Catch ex As Exception
                    Throw ex

                End Try
            End If
            If e.CommandName.Equals("Insert") Then

                'Dim SelectedRow As GridViewRow = grdCampaignFilter.FooterRow

                'Dim newSeqNo As TextBox = DirectCast(SelectedRow.FindControl("txtNewInsertSeqNo"), TextBox)
                'Dim newSystemFilter As DropDownList = DirectCast(SelectedRow.FindControl("ddlInsertNewSystemFilter"), DropDownList)
                'Dim newAndOr As TextBox = DirectCast(SelectedRow.FindControl("txtInsertNewAndOr"), TextBox)

                'Dim campaignfilteritemTA As New LogginsLogDSTableAdapters.usp_CampaignFilterItemTableAdapter()
                'campaignfilteritemTA.Insert(grdCampaignFilter.SelectedValue, newSeqNo.Text, newSystemFilter.SelectedValue, newAndOr.Text, True)
                'grdCampaignFilterItem.DataBind()
                ObjectDataSource1.Insert()
                grdCampaignFilterItem.DataBind()
            End If
            If e.CommandName.Equals("CustomEdit") Then
                Dim selectedid = e.CommandArgument.ToString()
                Dim campaignfilteritemcheckTA As New LogginsLogDSTableAdapters.usp_CampaignFilterItemTableAdapter()
                Dim campaignfilteritemcheckDT As LogginsLogDS.usp_CampaignFilterItemDataTable
                campaignfilteritemcheckDT = campaignfilteritemcheckTA.GetData(selectedid)
                For Each campaignfilteritmRow As LogginsLogDS.usp_CampaignFilterItemRow In campaignfilteritemcheckDT
                    Dim valuex = campaignfilteritmRow.SeqNo
                    If valuex = 0 Then
                        InsertPopupExtender0.Show()
                        lblAction0.Text = "You can't edit 'No Filter' item."
                    Else
                        HiddenField1.Value = selectedid
                        ModalPopupExtender1.Show()
                        Try

                            Dim campaignfilteritemTA As New LogginsLogDSTableAdapters.usp_CampaignFilterItemTableAdapter()
                            Dim campaignfilteritemDT As LogginsLogDS.usp_CampaignFilterItemDataTable
                            campaignfilteritemDT = campaignfilteritemTA.GetData(selectedid)

                            For Each campainitemRow As LogginsLogDS.usp_CampaignFilterItemRow In campaignfilteritemDT
                                HiddenField3.Value = campainitemRow.SeqNo
                                DropDownList6.SelectedValue = campainitemRow.AndOr
                                'TextBox4.Text = campainitemRow.AndOr
                                DropDownList4.SelectedValue = campainitemRow.SystemFilterId
                            Next

                        Catch ex As Exception
                            Throw ex
                        End Try

                    End If
                Next

            End If
            lblMessage.Text = ""

        Else
            lblMessage.Text = "Please Enter Campaign Filter"
        End If
    End Sub

    Protected Sub ObjectDataSource1_Inserting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles ObjectDataSource1.Inserting
        Try
            'Dim newSeqNo As TextBox = grdCampaignFilterItem.FooterRow.FindControl("txtNewInsertSeqNo")
            Dim newSystemFilter As DropDownList = grdCampaignFilterItem.FooterRow.FindControl("ddlInsertNewSystemFilter")
            'Dim newAndOr As TextBox = grdCampaignFilterItem.FooterRow.FindControl("txtInsertNewAndOr")
            Dim newAndOr As DropDownList = grdCampaignFilterItem.FooterRow.FindControl("ddlInsertNewAndOr")

            Dim campaignfilteritemTA As New LogginsLogDSTableAdapters.usp_CampaignFilterItemSelectByCampaignFilterIdTableAdapter()
            Dim campaignfilteritemDT As LogginsLogDS.usp_CampaignFilterItemSelectByCampaignFilterIdDataTable
            campaignfilteritemDT = campaignfilteritemTA.GetData(grdCampaignFilter.SelectedValue)
            Dim check = From q In campaignfilteritemDT.AsEnumerable() Where q.SystemFilterId = newSystemFilter.SelectedValue Select q
            If check.Count.ToString() = CStr(0) Then
                e.InputParameters("CampaignFilterId") = grdCampaignFilter.SelectedValue
                e.InputParameters("SeqNo") = Convert.ToByte(1)
                e.InputParameters("SystemFilterId") = newSystemFilter.SelectedValue
                e.InputParameters("AndOr") = newAndOr.Text
                e.InputParameters("Active") = True
                lblMessage.Text = ""
                Dim seqnofixTA As New LogginsLogDSTableAdapters.SPTableAdapter()
                seqnofixTA.usp_CampaignFilterItem_FixSeqNo(grdCampaignFilter.SelectedValue)
            Else
                InsertPopupExtender0.Show()
                lblAction0.Text = "This item is already in the list"
            End If
            'If Not newAndOr.Text = "" Then

            'Else
            '    lblMessage.Text = "Please enter EndOr value"

            'End If

        Catch ex As Exception
            Throw ex
        End Try
        '
        UpdatePanel3.Update()
    End Sub

    Protected Sub grdCampaignFilter_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles grdCampaignFilter.SelectedIndexChanged
        grdCampaignFilterItem.Visible = True
        UpdatePanel3.Update()
        divSelectedFilterText.Visible = True
        UpdatePanel4.Update()
    End Sub

    Function CheckSelectedFilterDuplicates() As Integer
        Return 0
    End Function

    Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        Try
            Dim campaignfilteritemcheckTA As New LogginsLogDSTableAdapters.usp_CampaignFilterItemSelectByCampaignFilterIdTableAdapter()
            Dim campaignfilteritemcheckDT As LogginsLogDS.usp_CampaignFilterItemSelectByCampaignFilterIdDataTable
            campaignfilteritemcheckDT = campaignfilteritemcheckTA.GetData(grdCampaignFilter.SelectedValue)
            Dim check = From q In campaignfilteritemcheckDT.AsEnumerable() Where q.SystemFilterId = DropDownList4.SelectedValue Select q
            If check.Count.ToString() = CStr(0) Then
                Dim campaignfilteritemTA As New LogginsLogDSTableAdapters.usp_CampaignFilterItemTableAdapter()
                campaignfilteritemTA.Update(HiddenField1.Value, grdCampaignFilter.SelectedValue, HiddenField3.Value, DropDownList4.SelectedValue, DropDownList6.SelectedValue, True)
                Dim seqnofixTA As New LogginsLogDSTableAdapters.SPTableAdapter()
                seqnofixTA.usp_CampaignFilterItem_FixSeqNo(grdCampaignFilter.SelectedValue)
                grdCampaignFilterItem.DataBind()

            Else
                InsertPopupExtender0.Show()
                lblAction0.Text = "This item is already in the list"

            End If

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub grdCampaignFilter_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdCampaignFilter.RowCommand
        If e.CommandName.Equals("CustomEdit") Then
            Dim selectedid = e.CommandArgument.ToString()
            HiddenField2.Value = selectedid
            ModalPopupExtender2.Show()
            Try

                Dim campaignfilterTA As New LogginsLogDSTableAdapters.usp_CampaignFilterTableAdapter()
                Dim campaignfilterDT As LogginsLogDS.usp_CampaignFilterDataTable
                campaignfilterDT = campaignfilterTA.GetData(selectedid)

                For Each campainitemRow As LogginsLogDS.usp_CampaignFilterRow In campaignfilterDT
                    TextBox5.Text = campainitemRow.Name
                    TextBox6.Text = campainitemRow.Note
                Next

            Catch ex As Exception
                Throw ex
            End Try

        End If

    End Sub

    Protected Sub Button5_Click(sender As Object, e As System.EventArgs) Handles Button5.Click
        Try

            Dim campaignfilterTA As New LogginsLogDSTableAdapters.usp_CampaignFilterTableAdapter()
            campaignfilterTA.Update(HiddenField2.Value, ddlEntity.SelectedValue, TextBox5.Text, TextBox6.Text, True)
        Catch ex As Exception
            Throw ex
        End Try
        grdCampaignFilter.DataBind()
    End Sub

    Protected Sub grdCampaignFilterItem_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles grdCampaignFilterItem.RowDeleting
        Dim id As Integer = CInt(grdCampaignFilterItem.DataKeys(e.RowIndex).Value)
        Dim campaignfilteritemTA As New LogginsLogDSTableAdapters.usp_CampaignFilterItemTableAdapter()
        Dim campaignfilteritemDT As LogginsLogDS.usp_CampaignFilterItemDataTable
        campaignfilteritemDT = campaignfilteritemTA.GetData(id)
        For Each campaignfilteritmRow As LogginsLogDS.usp_CampaignFilterItemRow In campaignfilteritemDT
            If campaignfilteritmRow.SystemFilterId = 0 Then
                InsertPopupExtender0.Show()
                lblAction0.Text = "You can't remove 'No Filter' item."
                e.Cancel = True
            End If
        Next
    End Sub
End Class
